package com.imooc2.designPatterns.adaptor.service.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imooc2.designPatterns.adaptor.dto.NewMethodDTO;
import com.imooc2.designPatterns.adaptor.service.NewService;
import com.imooc2.designPatterns.adaptor.vo.NewMethodVO;

/**
 * 适配者
 */
public class Adapter implements NewService {

    private OldServiceImpl oldServiceImpl;

    public Adapter(OldServiceImpl oldServiceImpl) {
        this.oldServiceImpl = oldServiceImpl;
    }

    @Override
    public NewMethodVO newMethod(NewMethodDTO newMethodDTO) {
        Map<String,String> param = JSON.parseObject(JSON.toJSONString(newMethodDTO), new TypeReference<Map<String, String>>() {});
        //调用旧接口
        Map<String,String> result = oldServiceImpl.oldMethod(param);
        NewMethodVO newMethodVO = JSON.parseObject(JSON.toJSONString(result), new TypeReference<NewMethodVO>(){});
        return newMethodVO;
    }

}
